<template>
  <div>
    <!-- 表格公用组件 -->
    <div class="i-table">
      <!-- 表格上方按钮区域 -->
      <div class="btn-list" v-show="toolbar.length">
        <el-button
          v-for="(item,index) in toolbar"
          :key="index"
          :type="item.type"
          @click="item.func"
        >{{item.btnName}}</el-button>
      </div>
    </div>
    <div class="table-container">
      <!-- 表格内容区域 -->
      <el-table :data="tableData" border @selection-change="handleSelectionChange" >
        <el-table-column v-if="selectionShow" type="selection" align="center"></el-table-column>
        <slot></slot>
      </el-table>
    </div>
    <!-- 表格下方分页区域 -->
    <div class="table-pagination" v-show="tableData.length">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageInfo.pageIndex"
        :page-sizes="[5,10,20,50]"
        :page-size="pageInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageInfo.total"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: "i-table",
  // 从父组件接收按钮设置项
  props: {
    toolbar: {
      type: Array,
      default: () => {
        return [];
      }
    },
    // 从父组件接收表格数据
    tableData: {
      type: Array,
      default: () => {
        return [];
      }
    },
    // 从父组件接收分页数据
    pageInfo: {
      type: Object,
      default: () => {
        return {
          pageIndex: 1,
          pageSize: 5,
          total: 0
        };
      }
    },
    selectionShow: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // loading: false
    };
  },
  methods: {
    handleSizeChange(value) {
      // 把每页数据条数事件发送给父组件
      this.$emit("handleSizeChange", value);
    },
    handleCurrentChange(value) {
      this.$emit("handleCurrentChange", value);
    },
    handleSelectionChange(rows) {
      this.$emit("selection-change", rows);
    }
  },
  components: {}
};
</script>

<style lang="scss" scoped>
.i-table {
  padding: 10px;
}
.el-pagination {
  text-align: right;
}
td{
/deep/.cell{
   text-overflow: ellipsis;
        white-space: nowrap;
        overflow: hidden;
}
}

.el-table td, .el-table th{
  padding: 0;
}
</style>
